import { request } from '@/utils/request';

// 房态枚举
export enum RoomStatus {
  VACANT = 'vacant', // 空置
  RESERVED = 'reserved', // 已预订
  RENTED = 'rented', // 已出租
  MAINTENANCE = 'maintenance', // 维修中
}

// 朝向枚举
export enum Orientation {
  NORTH = 'north', // 北
  SOUTH = 'south', // 南
  EAST = 'east', // 东
  WEST = 'west', // 西
  NORTHEAST = 'northeast', // 东北
  NORTHWEST = 'northwest', // 西北
  SOUTHEAST = 'southeast', // 东南
  SOUTHWEST = 'southwest', // 西南
}

// 装修风格枚举
export enum DecorationStyle {
  SIMPLE = 'simple', // 简装
  MEDIUM = 'medium', // 中装
  LUXURY = 'luxury', // 精装
  DELUXE = 'deluxe', // 豪装
}

// 出租类型枚举
export enum RentType {
  WHOLE = 'whole', // 整租
  SHARED = 'shared', // 合租
}

// 价格区间枚举
export enum PriceRange {
  LOW = 'low', // 低价
  MEDIUM = 'medium', // 中价
  HIGH = 'high', // 高价
  LUXURY = 'luxury', // 豪华
}

// IOT设备状态枚举
export enum DeviceStatus {
  ONLINE = 'online', // 在线
  OFFLINE = 'offline', // 离线
  ERROR = 'error', // 故障
  MAINTENANCE = 'maintenance', // 维护中
}

// IOT设备接口
export interface IOTDevice {
  id: string;
  name: string;
  type: string;
  status: DeviceStatus;
  lastUpdate: string;
  battery?: number;
  signal?: number;
}

// 房源信息接口
export interface RoomInfo {
  id: string;
  roomNumber: string;
  area: number;
  orientation: Orientation;
  facilities: string[];
  rent: number;
  deposit: number;
  images: string[];
  status: RoomStatus;
  decorationStyle: DecorationStyle;
  priceRange: PriceRange;
  rentType: RentType;
  tags: string[];
  iotDevices: IOTDevice[];
  description?: string;
  address?: string;
  floor?: number;
  building?: string;
  createdAt: string;
  updatedAt: string;
}

// 筛选条件接口
export interface RoomFilter {
  status?: RoomStatus;
  orientation?: Orientation;
  decorationStyle?: DecorationStyle;
  priceRange?: PriceRange;
  rentType?: RentType;
  building?: string;
  keyword?: string;
}

// 房源统计数据接口
export interface RoomStatistics {
  totalRooms: number;
  vacantRooms: number;
  reservedRooms: number;
  rentedRooms: number;
  maintenanceRooms: number;
  totalRent: number;
  averageRent: number;
  onlineDevices: number;
  totalDevices: number;
}

// 获取房源列表
export function getRoomList(params?: RoomFilter) {
  return request.get<RoomInfo[]>('/hotel/room-management/list', { params });
}

// 获取房源详情
export function getRoomDetail(roomId: string) {
  return request.get<RoomInfo>(`/hotel/room-management/${roomId}`);
}

// 新增房源
export function createRoom(data: Partial<RoomInfo>) {
  return request.post<RoomInfo>('/hotel/room-management', data);
}

// 更新房源信息
export function updateRoom(roomId: string, data: Partial<RoomInfo>) {
  return request.put<RoomInfo>(`/hotel/room-management/${roomId}`, data);
}

// 删除房源
export function deleteRoom(roomId: string) {
  return request.delete(`/hotel/room-management/${roomId}`);
}

// 批量删除房源
export function batchDeleteRooms(roomIds: string[]) {
  return request.delete('/hotel/room-management/batch', { data: { roomIds } });
}

// 获取房源统计数据
export function getRoomStatistics() {
  return request.get<RoomStatistics>('/hotel/room-management/statistics');
}

// 上传房源图片
export function uploadRoomImage(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  return request.post<string>('/hotel/room-management/upload-image', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

// 获取IOT设备列表
export function getIOTDevices(roomId: string) {
  return request.get<IOTDevice[]>(
    `/hotel/room-management/${roomId}/iot-devices`,
  );
}

// 添加IOT设备
export function addIOTDevice(roomId: string, device: Partial<IOTDevice>) {
  return request.post<IOTDevice>(
    `/hotel/room-management/${roomId}/iot-devices`,
    device,
  );
}

// 更新IOT设备
export function updateIOTDevice(
  roomId: string,
  deviceId: string,
  device: Partial<IOTDevice>,
) {
  return request.put<IOTDevice>(
    `/hotel/room-management/${roomId}/iot-devices/${deviceId}`,
    device,
  );
}

// 删除IOT设备
export function deleteIOTDevice(roomId: string, deviceId: string) {
  return request.delete(
    `/hotel/room-management/${roomId}/iot-devices/${deviceId}`,
  );
}

// 获取楼栋列表
export function getBuildingList() {
  return request.get<{ label: string; value: string }[]>(
    '/hotel/room-management/buildings',
  );
}

// 获取配套设施选项
export function getFacilityOptions() {
  return request.get<{ label: string; value: string }[]>(
    '/hotel/room-management/facilities',
  );
}

// 获取房源标签建议
export function getTagSuggestions() {
  return request.get<string[]>('/hotel/room-management/tag-suggestions');
}

// 导出房源数据
export function exportRoomData(params?: RoomFilter) {
  return request.get('/hotel/room-management/export', {
    params,
    responseType: 'blob',
  });
}

// 导入房源数据
export function importRoomData(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  return request.post('/hotel/room-management/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

// 房源状态变更
export function changeRoomStatus(roomId: string, status: RoomStatus) {
  return request.put(`/hotel/room-management/${roomId}/status`, { status });
}

// 批量更新房源状态
export function batchUpdateRoomStatus(roomIds: string[], status: RoomStatus) {
  return request.put('/hotel/room-management/batch-status', {
    roomIds,
    status,
  });
}
